var fs = require('fs');

fs.open(
        'info.txt', 'r',
        function (err, handle) {
            if (err) {
                console.log("ERROR: " + err.code
                        + " (" + err.message + ")");
                return;
            }
            var buf = new Buffer(100000);
            fs.read(
                    handle, buf, 0, 100000, null,
                    function(err, length) {
                        if (err) {
                            console.log("ERROR: " + err.code
                                    + " (" + err.message + ")");
                            return;
                        }
                        console.log(buf.toString('utf8', 0, length));
                        fs.close(handle, function() {});
                    }
            );
        }
);

function FileObject () {
    this.filename = '';

    this.file_exists = function (callback) {
        var self = this;
        console.log("About to open: " + self.filename);
        fs.open(this.filename, 'r', function(err, handle) {
            if (err) {
                console.log("Can't open: " + self.filename);
                callback(err);
                return;
            }
            fs.close(handle, function() {} );
            callback(null, true);
        })
    };
}

var fo = new FileObject();
fo.filename = "file_that_does_not_exist";

fo.file_exists(function (err, results) {
    if (err) {
        console.log("Aw, bummer: " + JSON.stringify(err));
        return;
    }

    console.log("file exists!!!");
});

function compute_intersection(arr1, arr2, callback) {
    // let's break up the bigger of the two arrays
    var bigger = arr1.length > arr2.length ? arr1 : arr2;
    var smaller = bigger == arr1 ? arr2 : arr1;
    var biglen = bigger.length;
    var smlen = smaller.length;

    var sidx = 0;           // starting index of any chunk
    var size = 10;          // chunk size, can adjust!
    var results = [];       // intermediate results

    // for each chunk of "size" elements in bigger, search through smaller
    function sub_compute_intersection() {
        for (var i = sidx; i < (sidx + size) && i < biglen; i++) {
            for (var j = 0; j < smlen; j++) {
                if (bigger[i] == smaller[j]) {
                    results.push(smaller[j]);
                    break;
                }
            }
        }

        if (i >= biglen) {
        callback(null, results);    // no error, send back results
        } else {
            sidx += size;
            process.nextTick(sub_compute_intersection);
        }
    }

    sub_compute_intersection();
}

var a1 = [ 879, 456465, 54, 1321, 2132, 21321, 546, 8797, 858, 2582, 4648, 2135,
    767654, 454, 2324, 345, 1234545, 899878, 345345, 1234, 807875, 2342];
var a2 = [ 2, 564, 548, 821346, 654, 87984, 3896, 14987, 321354, 84731, 1654798,
    8796465, 751442, 357, 368636, 8434, 1541498, 34574, 02154, 57, 8, 1, 3, 9,];

compute_intersection(a1, a2, function (err, results) {
    if (err) {
        console.log(err);
    } else {
        console.log(results);
    }
});

var fs = require('fs');

var handle = fs.openSync('info.txt', 'r');
var buf = new Buffer(100000);
var read = fs.readSync(handle, buf , 0, 100000, null);
console.log(buf.toString('utf8', 0, read));
fs.closeSync(handle);
